<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <!-- <el-form-item label="分组编码" prop="groupCode">
        <el-input
          v-model="queryParams.groupCode"
          placeholder="请输入分组编码"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="分组名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分组名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="分组状态"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['asset:deviceGroup:set']"
          >新增</el-button
        >
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['asset:deviceGroup:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['asset:deviceGroup:remove']"
          >删除</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['asset:deviceGroup:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="groupList"
      
      @selection-change="handleSelectionChange"
    >
    <!-- 序号 -->
    <el-table-column label="序号" width="60" align="center" type="index" />
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="分组编号" align="center" prop="groupId" /> -->
      <!-- <el-table-column label="分组编码" align="center" prop="groupCode" /> -->
      <el-table-column label="分组名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <!-- <el-table-column label="分组排序" align="center" prop="order" /> -->
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- 创建人 -->
      <el-table-column
        label="创建人"
        align="center"
        prop="create_user_name"
        width="180"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ scope.row.create_user_name }}</span>
        </template>
      </el-table-column>
      <!-- 更新时间 -->
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <!-- 更新人 -->
      <el-table-column
        label="更新人"
        align="center"
        prop="update_user_name"
        width="180"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ scope.row.update_user_name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['asset:deviceGroup:set']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['asset:deviceGroup:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分组对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="groupRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分组名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分组名称" />
        </el-form-item>
        <!-- <el-form-item label="排序" prop="order">
          <el-input-number
            v-model="form.order"
            controls-position="right"
            :min="1"
          />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Group">
import {
  listGroup,
  addGroup,
  delGroup,
  getGroup,
  updateGroup,
} from "@/api/asset/group";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const groupList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const submitLoading = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    // groupCode: undefined,
    // groupName: undefined,
    // status: undefined,
  },
  rules: {
    name: [{ required: true, message: "分组名称不能为空", trigger: "blur" }],
    // order: [
    //   { required: true, message: "排序不能为空", trigger: "blur" },
    // ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询分组列表 */
function getList() {
  loading.value = true;
  listGroup(queryParams.value).then((response) => {
    groupList.value = response.data.rows;
    total.value = response.data.total ;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    groupId: undefined,
    // groupCode: undefined,
    name: undefined,
    // order: 1,
    // status: "0",
    remark: undefined,
  };
  proxy.resetForm("groupRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.groupId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加分组";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = Object.assign({}, form.value, row);
  open.value = true;
    title.value = "修改分组";
  // const groupId = row.groupId || ids.value;

  // getGroup(groupId).then((response) => {
  //   form.value = response.data;
  //   open.value = true;
  //   title.value = "修改分组";
  // });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["groupRef"].validate((valid) => {
    if (valid) {
      submitLoading.value = true;
      if (form.value.groupId != undefined) {
        addGroup(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).catch(() => {
          
        }).finally(() => {
          submitLoading.value = false;
        });
      } else {
        form.value.groupId = "0"
        addGroup(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).catch(() => {
          
        }).finally(() => {
          submitLoading.value = false;
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const groupIds = row.groupId || ids.value;
  proxy.$modal
    .confirm('是否确认删除分组编号为"' + groupIds + '"的数据项？')
    .then(function () {
      return delGroup(groupIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/post/export",
    {
      ...queryParams.value,
    },
    `post_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
